package fms.mqAccessUtil;

import fms.controller.ISender;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.log4j.Logger;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import javax.jms.*;

public class ActivemqSender implements ISender {

    private Logger logger = Logger.getLogger(ActivemqSender.class);
    // 负责消息的传递、搬运
    private JmsTemplate jmsTemplate;
    // 作为发送者、生产者的ActiveMQQueue标识着“生产地址、仓库”；
    private ActiveMQQueue destination;

    public void send(final String aContent) {
        logger.info("[ActiveMQ 上行报文] 开始发送上行报文！");
//        System.out.println("[ActiveMQ 上行报文] 发送上行报文...");
        jmsTemplate.send(jmsTemplate.getDefaultDestinationName(),new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage message = session.createTextMessage(aContent);
                logger.info("[ActiveMQ 上行报文] 完成上行报文发送，内容为：\r\n" +  message.getText());
//                System.out.println("[ActiveMQ 上行报文] 发送内容为:" + message);
                return message;
            }
        });
    }

    public void setJmsTemplate(JmsTemplate jmsTemplate) {
        this.jmsTemplate = jmsTemplate;
    }

    public void setDestination(ActiveMQQueue destination) {
        this.destination = destination;
    }
}